/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Modulo3;

import Modelo.DAL.ConexionBD;
import Modelo.DAL.Edificio_DAO;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.json.JSONArray;
import org.json.JSONObject;

/**
 *
 * @author glpc
 */
public class listaEdificioAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String FAILURE = "failure";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    /*
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response)
    throws Exception {

    //    String nombre = request.getParameter("nombre");
    //    String siglas = request.getParameter("siglas");
    //    String offset = request.getParameter("offset");
    //    String limit = request.getParameter("limit");
    //
    //    String where = "SELECT *,(Select count(*) from edificio) FROM edificio where 1 = 1 ";
    //
    //    if (nombre != null && !nombre.equals("")) {
    //      where += " and nombre_edificio = '" + nombre + "'";
    //    }
    //
    //    if (siglas != null && !siglas.equals("")) {
    //      where += " and siglas = '" + siglas + "'";
    //    }
    //
    //    if (!limit.equals("0")) {
    //      where += " limit " + limit + " offset " + offset;
    //    }

    response.setContentType("text/json");
    JSONObject edificios = Edificio_DAO.consultarEdificioTabla(where);
    if (edificios == null) {
    return mapping.findForward(FAILURE);
    }

    JSONObject ext = new JSONObject();
    ext.put("consultarEdificio", edificios);
    PrintWriter pw = response.getWriter();
    pw.write(ext.toString());
    pw.flush();
    pw.close();

    return mapping.findForward(SUCCESS);
    }
    }
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        response.setContentType("text/json");
        JSONObject data = new JSONObject();
        data.put("Edificio", obtenerlistaEdificios());

        PrintWriter pw = response.getWriter();
        pw.write(data.toString());
        pw.flush();
        pw.close();

        return mapping.findForward(SUCCESS);
    }

    public JSONArray obtenerlistaEdificios() {
        ConexionBD conex = new ConexionBD();
        JSONArray edificios = new JSONArray();
        try {
            Statement st = conex.session.createStatement();
            ResultSet rs = st.executeQuery("SELECT nombre_edificio FROM edificio order by nombre_edificio desc;");
            int i = 0;
            while (rs.next()) {
                JSONObject edificio = new JSONObject();
                edificio.put("edificio", rs.getString(1));
                edificios.put(i++, edificio);
            }
        } catch (Exception e) {
        }
        return edificios;
    }
}
